package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Admin;
import com.sky.service.AdminService;
import com.sky.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 控制器-管理管理员信息页面
 * @author 尹稳健~
 * @version 1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    //注入业务对象
    @Autowired
    @Qualifier("adminServiceImpl")
    private AdminService adminService;

    //存储预返回页面的结果对象
    private Map<String,Object> result = new HashMap<String, Object>();

    /**
     * @description: 跳转到管理员信息管理页面
     * @param: no
     * @return java.lang.String
     */
    @RequestMapping("/goAdminList")
    public String goAdminList(){
        return "/admin/adminList";
    }

    /**
     * @description: 分页查询:根据管理员姓名获取指定/所有管理员信息列表
     * @param: page 当前页码
     * @param: rows 列表行数
     * @param: username 管理员姓名
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/getAdminList")
    @ResponseBody
    public Map<String,Object> getAdminList(Integer page,Integer rows,String username){
        //设置每页的记录数
        PageHelper.startPage(page,rows);
        //获取查询的用户名
        List<Admin> adminList = adminService.getAdminList(username);
        //封装查询结果
        PageInfo<Admin> pageInfo = new PageInfo<Admin>(adminList);
        //获取当前页数据列表
        List<Admin> list = pageInfo.getList();
        //获取总记录数
        long total = pageInfo.getTotal();
        result.put("total",total);
        result.put("rows",list);
        return result;
    }

    /**
     * @description: 添加管理员信息
     * @param: admin
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/addAdmin")
    @ResponseBody
    public Map<String,Object> addAdmin(Admin admin){
        Admin adminByName = adminService.findAdminByName(admin);
        //判断用户名是否已存在
        if (adminByName != null){
            result.put("success",false);
            result.put("msg","该用户已存在!");
            return result;
        }
        //添加操作
        adminService.addAdmin(admin);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 根据id修改指定管理员信息
     * @param: admin
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/editAdmin")
    @ResponseBody
    public Map<String,Object> editAdmin(Admin admin){
        Admin adminByName = adminService.findAdminByName(admin);
        //需排除用户只修改账户名以外的信息
        if (adminByName!=null){
            if (!adminByName.getId().equals(admin.getId())){
                result.put("success",false);
                result.put("msg","用户名重复，请重新输入一个用户名!");
                return result;
            }
        }
        //修改操作
        adminService.editAdmin(admin);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 删除指定id的管理员信息
     * @param: ids 拼接后的id
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/deleteAdmin")
    @ResponseBody
    public Map<String,Object> deleteAdmin(@RequestParam(value = "ids[]",required = true) Integer[] ids){
        adminService.deleteAdmin(ids);
        result.put("success",true);
        return result;
    }

    /**
     * @description: 上传头像-原理:将头像上传到项目发布目录中,通过读取数据库中的头像路径来获取头像
     * @param: photo
     * @param: request
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @RequestMapping("/uploadPhoto")
    @ResponseBody
    public Map<String,Object> uploadPhoto(MultipartFile photo,HttpServletRequest request){
        //存储头像的本地目录
        final String dirPath = request.getSession().getServletContext().getRealPath("/upload/admin_portrait/");
        System.out.println(dirPath);
        //存储头像的项目发布目录
        final String portraitPath = request.getSession().getServletContext().getContextPath() + "/upload/admin_portrait/";
//        System.out.println(portraitPath);
        //返回头像的上传结果
        return UploadFile.getUploadResult(photo, dirPath, portraitPath);
    }

}
